package com.test;

import com.test.mapper.TestMapper;
import com.test.model.Test;
import com.test.model.TestOrder;
import com.test.model.OrderState;
import com.test.service.TestOrderService;
import com.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Map;
import java.util.Optional;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = "com.test.*")
@MapperScan("com.geektime.springbucks.mapper")
public class SpringBucksApplication implements ApplicationRunner {

    @Autowired
    private TestMapper testMapper;

    @Autowired
    private TestOrderService orderService;

    @Autowired
    private TestService testService;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;


    public static void main(String[] args) {
        SpringApplication.run(SpringBucksApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("All Test: {}", testMapper.find().toString());
        final Optional<Test> latte = testService.findOneTest("latte");
        if (latte.isPresent()) {
            final TestOrder order = orderService.createOrder("colin", latte.get());
            log.info("Update INIT TO PAID: {}", orderService.updateState(order, OrderState.PAID));
            log.info("Update PAID TO INIT: {}", orderService.updateState(order, OrderState.INIT));
        }
        fenye();
        redis();
    }

    public void fenye(){

        testMapper.findAllWithRowBounds(new RowBounds(0, 3)).forEach(c -> log.info("Test {}", c));
        testMapper.findAllWithRowBounds(new RowBounds(3, 3)).forEach(c -> log.info("Test {}", c));
        testMapper.findAllWithRowBounds(new RowBounds(1, 0)).forEach(c -> log.info("Test {}", c));
    }

    public void redis(){
        JedisPoolConfig  jedisPoolConfig= new JedisPoolConfig();
        log.info("PoolConfig - : {}" , jedisPoolConfig.toString());
        JedisPool jedisPool = new JedisPool(host,port);
        try (Jedis jedis = jedisPool.getResource()) {

            testMapper.find().forEach(c -> { jedis.hset("springbucks-menu", c.getName(), Long.toString(c.getPrice())); });
            Map<String, String> menu = jedis.hgetAll("springbucks-menu");
            log.info("Menu: {}", menu);
            String price = jedis.hget("springbucks-menu", "espresso");
            log.info("espresso - {}", Long.parseLong(price));
        }
    }
}
